/*
 * MyColors.java
 * Nov 17, 2009 1:17:27 PM
 *
 */
package colors_flow;

import java.awt.Color;
import java.util.ArrayList;

/**
 *
 * @author thirdy
 */
public class MyColors {

    public static Color createColor(String hexColorStr) {
        // remove '#'
        String s = hexColorStr.substring(1);
        System.out.println(s);
        int r = Integer.parseInt(s.substring(0, 2), 16);
        System.out.println(r);
        int g = Integer.parseInt(s.substring(2, 4), 16);
        System.out.println(g);
        int b = Integer.parseInt(s.substring(4, 6), 16);
        System.out.println(b);
        return new Color(r, g, b);
    }

    public static String getHex(Color color){
        int red = color.getRed();
        int green = color.getGreen();
        int blue = color.getBlue();
        return "#" + Integer.toHexString(red).toUpperCase() + (red == 0 ? "0" : "") +
                Integer.toHexString(green).toUpperCase() + (green == 0 ? "0" : "") +
                Integer.toHexString(blue).toUpperCase() + (blue == 0 ? "0" : "");
    }
    
    static java.awt.Color BLUE = new Color(0, 0, 255);
    static java.awt.Color BLUE_GREEN = new Color(0, 255, 255);     // #00FFFF (Cyan)
    static java.awt.Color BLUE_VIOLET = new Color(138, 43, 226);   // #8A2BE2 (X11/HTML/CSS)
    static java.awt.Color GREEN = new Color(0, 255, 0);
    static java.awt.Color YELLOW = new Color(255, 255, 0);
    static java.awt.Color YELLOW_GREEN = new Color(127, 255, 0);
    static java.awt.Color ORANGE = new Color(255, 127, 0);         // #FF7F00
    static java.awt.Color RED = new Color(255, 0, 0);
    static java.awt.Color RED_ORANGE = new Color(255, 63, 0);      // #FF3F00
    static java.awt.Color RED_VIOLET = new Color(199, 21, 133);    // #C71585
    static java.awt.Color VIOLET = new Color(238, 130, 238);       // #EE82EE
    static java.awt.Color YELLOW_ORANGE = new Color(255, 215, 0);  // #FFD700 (Gold)

//    static public ArrayList<Color> colorOrigin(Color c) {
//        ArrayList<Color> al = new ArrayList<Color>();
//        if (c.equals(BLUE_GREEN)) {
//            al.add(BLUE);
//            al.add(GREEN);
//        }
//        if (c.equals(BLUE_VIOLET)) {
//            al.add(BLUE);
//            al.add(VIOLET);
//        }
//        if (c.equals(YELLOW_GREEN)) {
//            al.add(YELLOW);
//            al.add(GREEN);
//        }
//        if (c.equals(YELLOW_ORANGE)) {
//            al.add(YELLOW);
//            al.add(ORANGE);
//        }
//        if (c.equals(RED_ORANGE)) {
//            al.add(RED);
//            al.add(ORANGE);
//        }
//        if (c.equals(RED_VIOLET)) {
//            al.add(RED);
//            al.add(VIOLET);
//        } else {
//            al.add(c);
//        }
//        return al;
//    }

    static public Color colorMixer(Color c1, Color c2) {

        int i = c1.getRed() + c2.getRed();
        int j = c1.getGreen() + c2.getGreen();
        int k = c1.getBlue() + c2.getBlue();

        int l = Math.max(Math.max(i, j), k);
        if (l > 0) {
            i = 255 * i / l;
            j = 255 * j / l;
            k = 255 * k / l;
        }
        Color localColor1 = new Color(i, j, k);

        System.out.println("[" + i + "," + j + "," + k + "]");
        return localColor1;
    }

    // <editor-fold defaultstate="collapsed" desc="NOT USED, another algorithm for mixing colors">
    static public Color colorMixer2(Color c1, Color c2) {
        double d13;
        double d1 = 1.0D - (c1.getRed() / 255.0D);
        double d2 = 1.0D - (c2.getRed() / 255.0D);

        double d3 = 1.0D - (c1.getGreen() / 255.0D);
        double d4 = 1.0D - (c2.getGreen() / 255.0D);

        double d5 = 1.0D - (c1.getBlue() / 255.0D);
        double d6 = 1.0D - (c2.getBlue() / 255.0D);

        double d7 = percentWhite(d1, d3, d5);
        double d8 = percentWhite(d2, d4, d6);
        double d9 = (d7 + d8) / 2.0D;

        double d10 = d1 + d2;
        double d11 = d3 + d4;
        double d12 = d5 + d6;

        if (d9 > 0.0D) {
            d13 = Math.max(Math.max(d10, d11), d12);
            d13 = (1.0D - d9) / d13;
            d10 *= d13;
            d11 *= d13;
            d12 *= d13;
        }

        if ((d10 > 1.0D) || (d12 > 1.0D) || (d11 > 1.0D)) {
            d13 = Math.max(Math.max(d10, d11), d12);
            d10 /= d13;
            d11 /= d13;
            d12 /= d13;
        }

        int i1 = (int) (255.0D - (d10 * 255.0D));
        int i2 = (int) (255.0D - (d11 * 255.0D));
        int i3 = (int) (255.0D - (d12 * 255.0D));

        return new Color(i1, i2, i3);
    }

    static double percentWhite(double paramDouble1, double paramDouble2, double paramDouble3) {
        double d1 = 1.0D;
        d1 = Math.max(Math.max(paramDouble1, paramDouble3), paramDouble2);
        double d2 = 1.0D - d1;

        double d3 = 0.0D;

        if ((paramDouble1 == 1.0D) || (paramDouble2 == 1.0D) || (paramDouble3 == 1.0D)) {
            d2 = 0.0D;
        } else if ((paramDouble1 == paramDouble3) && (paramDouble3 == paramDouble2)) {
            d2 = 1.0D - paramDouble1;
        } else if ((paramDouble1 == 0.0D) || (paramDouble3 == 0.0D) || (paramDouble2 == 0.0D)) {
            if (paramDouble1 == 0.0D) {
                d3 = Math.min(paramDouble3, paramDouble2);
                d1 = Math.max(paramDouble3, paramDouble2);
            } else if (paramDouble3 == 0.0D) {
                d3 = Math.min(paramDouble1, paramDouble2);
                d1 = Math.max(paramDouble1, paramDouble2);
            } else if (paramDouble2 == 0.0D) {
                d3 = Math.min(paramDouble1, paramDouble3);
                d1 = Math.max(paramDouble1, paramDouble3);
            }
            d2 = 1.0D - (Math.sqrt(d3 * d3 + d1 * d1) / Math.sqrt(d3 * d3 / d1 * d1 + 1.0D));
        } else {
            d1 = Math.max(Math.max(paramDouble1, paramDouble3), paramDouble2);
            d2 = 1.0D - d1;
        }

        return d2;
    }

// </editor-fold>
}
